{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#1）找一个自己电脑上文件较多、文件夹层数较多的工作文件夹，分别打印列出该文件夹（及其子文件夹）中的word, ppt, 和 excel文件的绝对路径；\n",
    "#2）装上述word, ppt, 和 excel分别复制到一个独立的文件夹中。\n",
    "#没学如何打开文件夹以及让打印文件的绝对路径\n",
    "import os\n",
    "path_list = []\n",
    "def get_all(path):\n",
    "    #path =r'D:Test3'\n",
    "    paths = os.listdir(path) # 列出指定路径下的所有目录和文件\n",
    "    for i in paths:\n",
    "        com_path = os.path.join(path,i)\n",
    "        # print(com_path)\n",
    "        if os.path.isdir(com_path):\n",
    "            get_all(com_path) # 如果该路径是目录，则调用自身方法\n",
    "        elif os.path.isfile(com_path):\n",
    "            path_list.append(com_path) # 如果该路径是文件，则追加到path_list中\n",
    "            # print(com_path) 打印所有文件的绝对路径\n",
    "        print(com_path) # 打印所有文件和目录的绝对路径\n",
    "print(\"D:Test3包括如下文件：\")\n",
    "# 调用函数\n",
    "get_all(path=r'D:Test3')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "total 2 to rename & converted 1 png\n"
     ]
    }
   ],
   "source": [
    "#批量修改文件后缀\n",
    "#将某一个文件夹下的所有.doc后缀的文件的文件名加上前缀\"DOC_\"进行重命名\n",
    "#没学，但我查到了，但也不太懂，那两个函数没学过\n",
    "import os\n",
    " \n",
    "class BatchRename():\n",
    " \n",
    "    def rename(self):\n",
    "        path = \"D:\\电子知识库\\期中-21本地化-202111680811-马群-语言的起源和肢体语言的含义\"\n",
    "        filelist = os.listdir(path)\n",
    "        total_num = len(filelist)\n",
    "        i = 0\n",
    "        for item in filelist:\n",
    "            if item.endswith('.docx'):\n",
    "                src = os.path.join(os.path.abspath(path), item)\n",
    "                dst = os.path.join(os.path.abspath(path), \"DOC_\"+'docx')\n",
    "                try:\n",
    "                    os.rename(src, dst)\n",
    "                    i += 1\n",
    "                except:\n",
    "                    continue\n",
    "        print('total %d to rename & converted %d png'%(total_num, i))\n",
    " \n",
    "if __name__=='__main__':\n",
    "    demo = BatchRename()\n",
    "    demo.rename()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.7 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "2dbe5485cf12282196b80a8ac362415f1d4830d1445e2229f35f94c570ef032e"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
